<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title="同步指纹"
               :modal="false"
               class="user-dialog add-or-updata"
               width="30%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <div class="title">说明,将设备上的指纹数据同步到本系统</div>
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-form-item label="选择设备"
                      prop="did"
                      :rules="required()">
          <el-select v-model="dataForm.did"
                     clearable
                     filterable
                     style="width:100%"
                     placeholder="请选择同步设备">
            <el-option v-for="item in syncEquList"
                       :key="item.id"
                       :label="item.deviceId + '(' + item.name + ')'"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t("cancel") }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">同步</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: '', // 添加或修改接口，API地址
        updateURL: '', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      visible: false,
      dataForm: {
        did: ''
      },
      syncEquList: []
    }
  },
  computed: {
  },
  components: { },
  methods: {
    init (data) {
      if (data) {
        this.dataForm.id = data
      }
      this.visible = true
      this.$nextTick(() => {
        this.$http.get('tb/device/listCheck?fingerprintDeviceConfigure=0').then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.syncEquList = res.data
        })
      })
    },
    closeDialog () {
    //   this.$refs.dataForm.resetFields()
      this.dataForm.id = ''
      this.dataForm.did = ''
      this.$emit('callBack')
      this.visible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$http.get('system/sysUserFinGerPrint/synchronizeFingerprint?userId=' + this.dataForm.id + '&did=' + this.dataForm.did).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message.success('保存成功')
              this.closeDialog()
            })
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style lang="scss" scoped>
.title {
  border-left: 6px solid #286bff;
  padding: 10px;
}
</style>
